public class Move 
{ 
	private IBoard board;
	private int row, col;
	private int score;
	
	public Move(IBoard brd, int r, int c)
	{
		this (brd, r, c, 1);
	}

	public Move(IBoard brd, int r, int c, int scr)
	{
		board = brd;
		
		// r and c may be negative, in which case they are treated as offsets from the far corner.
		// That is, (-1,-1) is the bottom-right corner that is more correctly 
		// referred to as (board.getRowCount()-1, board.getColCount()-1)
		while (r < 0)
			r += board.getRowCount();
		row = r;

		while (c < 0)
			c += board.getColCount();
		col = c;
		
		score = scr;

//		assert (row >= 0 && col >= 0);
	}

	/**
	 * I don't use it, but here's a constructor for storing a "pass" move.
	 */
	public Move(IBoard brd)
	{
		board = brd;
		row = col = -1;
		score = 0;
	}
	
	public int row()
	{
		return row;
	}

	public int col()
	{
		return col;
	}
	
	public int score()
	{
		return score;
	}
	
	/**
	 * In case caller which created this move object didn't check in advance ;)
	 */
	public boolean isValid()
	{
		// Negative coordinates mean pass.
		// Note that after constructor above, inverted coordinates should be gone.
		if (row < 0 || col < 0)
		{
			return true;
		}
		return board.isValidMove(board.getCurrentPlayerColor(), row, col);
	}

	/**
	 * @return true iff the move was valid.
	 */
	public boolean doMove(IPlayer player)
	{
		// Negative coordinates mean pass.
		// Note that after constructor above, inverted coordinates should be gone.
		if (row < 0 || col < 0)
		{
			board.pass(player);
			return true;
		}
		return board.placePiece(player, row, col);
	}
	
	/**
	 * @return the new board if the hypothetical move was valid.
	 * Return null if the move was invalid.
	 */
	public IBoard doHypothetical()
	{
		if (row < 0 || col < 0)
		{
//			assert (false);	// can't hypothetically pass
			return null;
		}
		return board.hypotheticallyPlacePiece(board.getCurrentPlayerColor(), row, col);
	}

	/**
	 * Finds all the valid moves for the given player on the given board.
	 */
	public static MoveList validMoves(IBoard board)
	{
		MoveList moves = new MoveList(board);
		
		// Find all valid moves.
		for (int r = 0; r < board.getRowCount(); r++)
		{
			for (int c = 0; c < board.getColCount(); c++)
			{
				if (board.isValidMove(board.getCurrentPlayerColor(), r, c))
				{
					// Make the first valid move we can find.
					moves.add(new Move(board, r, c));
				}
			}
		}
		
		return moves;
	}
} 
